package com.mes.utils;

import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

@SuppressWarnings("unchecked")
public class ReturnSheetUtils {
	public static JSONObject getSheet(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			json.put("result", false);
			request.setCharacterEncoding("utf-8");
			DiskFileItemFactory factory = new DiskFileItemFactory();
			//设置 缓存的大小，当上传文件的容量超过该缓存时，直接放到 暂时存储室  
			factory.setSizeThreshold(1024 * 1024);
			ServletFileUpload upload = new ServletFileUpload(factory);
			//可以上传多个文件  
			List<FileItem> listItem = (List<FileItem>) upload.parseRequest(request);
			for (FileItem item : listItem) {
				if (!item.isFormField()) {
					String name = item.getName();
					if (item.getSize() > 3 * 1024 * 1024) {
						json.put("desc", "文件大小不能超过3M");
						return json;
					}
					InputStream is = item.getInputStream(); //文件流
					try {
						Workbook wb = null;
						if (name.endsWith(".xls")) {
							wb = new HSSFWorkbook(is);
						} else if (name.endsWith(".xlsx")) {
							wb = new XSSFWorkbook(is);
						}
						if (wb != null) {
							json.put("sheet", wb.getSheetAt(0));
							json.put("flag", true);
							return json;
						} else {
							json.put("desc", "文件格式错误，至于许EXCEL的导入");
							json.put("flag", false);
							return json;
						}
					} catch (Exception e) {
						e.printStackTrace();
						json.put("desc", e.getMessage());
						json.put("flag", false);
					} finally {
						is.close();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			try {
				json.put("desc", e.getMessage());
				json.put("flag", false);
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
		}
		return json;
	}
	
	//	    CELL_TYPE_NUMERIC 数值型 0
	//		CELL_TYPE_STRING 字符串型 1
	//		CELL_TYPE_FORMULA 公式型 2
	//		CELL_TYPE_BLANK 空值 3
	//		CELL_TYPE_BOOLEAN 布尔型 4
	//		CELL_TYPE_ERROR 错误 5
	public static String readValue(Cell cell) throws Exception {
		if (cell == null) {
			return "";
		}
		if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
			Double text = cell.getNumericCellValue();
			return String.valueOf(text.longValue());
		} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
			return cell.getStringCellValue();
		} else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
			throw new Exception("第" + (cell.getRowIndex() + 1) + "行，" + (cell.getColumnIndex() + 1) + "列，数据格式有问题,请检查");
		} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
			return String.valueOf(cell.getBooleanCellValue());
		} else {
			return "";
		}
	}
}
